# Maintainer: David Runge <dvzrv@archlinux.org>

# TODO: devendor clap and minizip
pkgbase=spectmorph
pkgname=(spectmorph{,-{clap,instruments,lv2,tools,vst}} libspectmorph)
pkgver=0.6.1
pkgrel=1
pkgdesc="Analyze samples of musical instruments and combine them (morphing)"
arch=(loong64 x86_64)
url="https://spectmorph.org/"
license=(LGPL-2.1-or-later)
makedepends=(
  cairo
  fftw
  freetype2
  gcc-libs
  glib2
  glibc
  jack
  lv2
  libao
  libglvnd
  libsndfile
  qt5-base
)
checkdepends=(
  lv2lint
  python
)
source=(https://github.com/swesterfeld/spectmorph/releases/download/$pkgver/$pkgname-$pkgver.tar.bz2)
sha512sums=('6071c25a1034a0ae7aaf941aeaa9ae16d51c6d999802e8012da9db1cc47228f1b2bdbf5ba1bc3f96bea7d1f67c1b5cd1ad506f18e49b5d8f8e2ae8405272dc6a')
b2sums=('2a71a02794c227ee0ab80316327450640cbc48d40fd371229abcb8733312f3e64e2c9e0178606aa52faea2d69e6395a8b84dd50d9966b4814f635f8b0c43a1e0')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $pkgname-$pkgver
  autoreconf -fiv
}

build() {
  local configure_options=(
    --prefix=/usr
    --with-clap
    --with-lv2
    --with-jack
    --with-qt
  )

  cd $pkgname-$pkgver
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  make -k check -C $pkgname-$pkgver

  cd $pkgname-$pkgver
  LD_LIBRARY_PATH="$PWD/glui/.libs/:$PWD/lib/.libs/:$LD_LIBRARY_PATH" lv2lint -I lv2/ -Mpack "http://spectmorph.org/plugins/spectmorph"
}

package_spectmorph() {
  depends=(
    libspectmorph
    spectmorph-{clap,lv2,tools,vst}
  )

  make DESTDIR="$pkgdir/" install -C $pkgname-$pkgver

  (
    cd "$pkgdir"
    _pick lib$pkgbase usr/include
    _pick lib$pkgbase usr/lib/libspectmorph*
    _pick lib$pkgbase usr/lib/pkgconfig
    _pick lib$pkgbase usr/share/$pkgbase/templates

    _pick $pkgbase-clap usr/lib/clap
    _pick $pkgbase-instruments usr/share/$pkgbase/instruments

    _pick $pkgbase-lv2 usr/lib/lv2

    _pick $pkgbase-tools usr/bin
    _pick $pkgbase-tools usr/share

    _pick $pkgbase-vst usr/lib/vst
  )
}

package_spectmorph-clap() {
  pkgdesc+=" - CLAP plugin"
  groups=(
    pro-audio
    clap-plugins
  )
  depends=(
    clap-host
    gcc-libs
    glibc
    libspectmorph
  )
  optdepends=(
    'spectmorph-instruments: for standard instruments'
  )

  mv -v $pkgname/* "$pkgdir"
}

package_spectmorph-instruments() {
  pkgdesc+=" - standard instruments"

  mv -v $pkgname/* "$pkgdir"
}

package_libspectmorph() {
  pkgdesc+=" - libs"
  depends=(
    cairo
    gcc-libs
    glibc
    fftw libfftw3f.so libfftw3f_threads.so
    freetype2 libfreetype.so
    glib2 libglib-2.0.so
    jack libjack.so
    libglvnd libGL.so
    libsndfile libsndfile.so
    libx11
    qt5-base
    zlib
  )
  provides=(
    libspectmorphjack.so
    libspectmorphgui.so
    libspectmorphglui.so
    libspectmorph.so
  )

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,DESIGN,NEWS.md,README.md,TODO} -t "$pkgdir/usr/share/doc/$pkgbase/"
}

package_spectmorph-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    pro-audio
    lv2-plugins
  )
  depends=(
    gcc-libs
    glibc
    libspectmorph
    lv2-host
  )
  optdepends=(
    'spectmorph-instruments: for standard instruments'
  )

  mv -v $pkgname/* "$pkgdir"
}

package_spectmorph-tools() {
  pkgdesc+=" - tools"
  groups=(
    pro-audio
  )
  depends=(
    gcc-libs
    glibc
    glib2 libglib-2.0.so
    jack libjack.so
    libao
    libspectmorph
    qt5-base
    python
    python-lxml
  )
  optdepends=(
    'spectmorph-instruments: for standard instruments'
  )

  mv -v $pkgname/* "$pkgdir"
}

package_spectmorph-vst() {
  pkgdesc+=" - VST plugin"
  groups=(
    pro-audio
    vst-plugins
  )
  depends=(
    gcc-libs
    glibc
    libspectmorph
    vst-host
  )
  optdepends=(
    'spectmorph-instruments: for standard instruments'
  )

  mv -v $pkgname/* "$pkgdir"
}
